package com.bcbim.api_web.business_department.controller;

import com.bcbim.authentication_interface.util.AuthUtil;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.sys.Code;
import com.bcbim.common.sys.Constant;
import com.bcbim.common.sys.ParamException;
import com.bcbim.common.util.GuidUtil;
import com.bcbim.common.util.ParamCheck;
import com.bcbim.operation_maintenance_interface.business_department.dao.BusinessDepartmentDAO;
import com.bcbim.operation_maintenance_interface.business_department.service.BusinessDepartmentService;
import com.bcbim.operation_maintenance_interface.business_department.vo.BusinessDepartmentDetailVO;
import com.bcbim.operation_maintenance_interface.business_department.vo.BusinessDepartmentTreeVO;
import com.bcbim.operation_maintenance_interface.business_department.vo.ListBusinessDepartmentVO;
import com.bcbim.operation_maintenance_interface.department.dao.DepartmentDAO;
import com.bcbim.operation_maintenance_interface.department.service.DepartmentService;
import com.bcbim.operation_maintenance_interface.space.dto.SpaceUnassignedDTO;
import com.bcbim.operation_maintenance_interface.space.service.SpaceService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @version V 1.0.0
 * @Description：业务部controller
 * @Author: haomeijie
 * @Date: 2022/3/8 17:43
 **/
@RestController
@RequestMapping("/businessDepartment")
public class BusinessDepartmentController {

    @Reference(protocol = "dubbo", group = "department", version = "1.0.0")
    private DepartmentService departmentServiceImpl;

    @Reference(protocol = "dubbo", group = "businessDepartment", version = "1.0.0")
    private BusinessDepartmentService businessDepartmentServiceImpl;

    @Reference(protocol = "dubbo", group = "space", version = "1.0.0")
    private SpaceService spaceServiceImpl;

    /**
     * 业务部-部门树形列表
     *
     * @Author: haomeijie
     * @Date: 2022/3/8 17:48
     */
    @RequestMapping("/listDepartmentTree")
    public ReturnMsg listDepartmentTree(String projectId) {
        List<BusinessDepartmentTreeVO> list = businessDepartmentServiceImpl.listDepartmentTree(projectId);
//        List<ListBusinessDepartmentTreeVO> departmentTreeVOList = businessDepartmentServiceImpl.listSpaceByDepartment(new SpaceDTO());
//        List<BusinessDepartmentTreeVO> list = new ArrayList<>();
//        List<BusinessDepartmentTreeVO> childList;
//        //整理参数
//        for (ListBusinessDepartmentTreeVO departmentTreeVO : departmentTreeVOList) {
//            //业务部
//            BusinessDepartmentTreeVO businessDepartmentTreeVO = new BusinessDepartmentTreeVO();
//            businessDepartmentTreeVO.setId(departmentTreeVO.getBusinessDepartmentId());
//            businessDepartmentTreeVO.setName(departmentTreeVO.getBusinessDepartmentName());
//            businessDepartmentTreeVO.setParentId(null);
//            childList = new ArrayList<>();
//            //部门
//            if (ObjectUtils.isNotEmpty(departmentTreeVO.getChildList())) {
//                for (ListBusinessDepartmentTreeVO treeVO : departmentTreeVO.getChildList()) {
//                    BusinessDepartmentTreeVO departmentVO = new BusinessDepartmentTreeVO();
//                    departmentVO.setId(treeVO.getDepartmentId());
//                    departmentVO.setName(treeVO.getDepartmentName());
//                    departmentVO.setParentId(treeVO.getBusinessDepartmentId());
//                    departmentVO.setChild(null);
//                    childList.add(departmentVO);
//                }
//            }
//            businessDepartmentTreeVO.setChild(childList);
//            list.add(businessDepartmentTreeVO);
//        }
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 主管下拉
     *
     * @author xuehao
     * @createDate 2022/3/9 14:35
     * @retuen ReturnMsg
     */
    @PostMapping("pullUser")
    public ReturnMsg pullUser() {
        List<Code> list = businessDepartmentServiceImpl.pullUser();
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 新建业务部
     *
     * @param businessDepartmentDAO
     * @author xuehao
     * @createDate 2022/3/9 14:21
     * @retuen ReturnMsg
     */
    @PostMapping("/insertBusinessDepartment")
    public ReturnMsg insertBusinessDepartment(BusinessDepartmentDAO businessDepartmentDAO) {
        try {
            ParamCheck.stringNotEmpty(businessDepartmentDAO.getBusinessDepartmentName(), 50);
            //业务部主管改手输
            ParamCheck.stringCanBeEmpty(businessDepartmentDAO.getBusinessDepartmentDirector(), 32);
            ParamCheck.idCheck(businessDepartmentDAO.getProjectId());
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //判断是否重名
        if (businessDepartmentServiceImpl.checkBusinessDepartmentName(businessDepartmentDAO.getBusinessDepartmentName(),
                businessDepartmentDAO.getProjectId(), null) > 0) {
            return ReturnMsg.consFail("业务部名称重复");
        }
        businessDepartmentDAO.setId(GuidUtil.get());
        businessDepartmentDAO.setDel(Constant.NOT_DEL);
        businessDepartmentDAO.setCreateBy(AuthUtil.getUserIdDecrypt());
        businessDepartmentDAO.setCreateTime(LocalDateTime.now());
        int result = businessDepartmentServiceImpl.insertBusinessDepartment(businessDepartmentDAO);
        return result == 1 ? ReturnMsg.consSuccess(Constant.SAVE_SUCCESS) : ReturnMsg.consFail(Constant.SAVE_FAIL);
    }

    /**
     * 编辑业务部
     *
     * @param businessDepartmentDAO
     * @author xuehao
     * @createDate 2022/3/9 15:01
     * @retuen ReturnMsg
     */
    @PostMapping("/updateBusinessDepartment")
    public ReturnMsg updateBusinessDepartment(BusinessDepartmentDAO businessDepartmentDAO) {
        try {
            ParamCheck.idCheck(businessDepartmentDAO.getId());
            ParamCheck.stringNotEmpty(businessDepartmentDAO.getBusinessDepartmentName(), 50);
            //业务部主管改手输
            ParamCheck.stringCanBeEmpty(businessDepartmentDAO.getBusinessDepartmentDirector(), 32);
            //ParamCheck.idCanBeEmpty(businessDepartmentDAO.getBusinessDepartmentDirector());
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //判断是否同名
        if (businessDepartmentServiceImpl.checkBusinessDepartmentName(businessDepartmentDAO.getBusinessDepartmentName(),
                businessDepartmentDAO.getProjectId(), businessDepartmentDAO.getId()) > 0) {
            return ReturnMsg.consFail("业务部名称重复");
        }
        businessDepartmentDAO.setUpdateTime(LocalDateTime.now());
        businessDepartmentDAO.setUpdateBy(AuthUtil.getUserIdDecrypt());
        int result = businessDepartmentServiceImpl.updateBusinessDepartment(businessDepartmentDAO);
        return result == 1 ? ReturnMsg.consSuccess(Constant.SAVE_SUCCESS) : ReturnMsg.consFail(Constant.SAVE_FAIL);
    }

    /**
     * 删除业务部
     *
     * @param id
     * @author xuehao
     * @createDate 2022/3/9 15:13
     * @retuen ReturnMsg
     */
    @PostMapping("/deleteBusinessDepartment")
    public ReturnMsg deleteBusinessDepartment(String id, String projectId) {
        try {
            ParamCheck.idCheck(id);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        BusinessDepartmentDAO businessDepartmentDAO = new BusinessDepartmentDAO();
        //获取业务部的部门
        List<String> list = businessDepartmentServiceImpl.getDepartmentIds(id);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(string -> {
                DepartmentDAO departmentDAO = new DepartmentDAO();
                departmentDAO.setId(string);
                departmentDAO.setDel(Constant.DEL);
                departmentDAO.setUpdateTime(LocalDateTime.now());
                departmentDAO.setUpdateBy(AuthUtil.getUserIdDecrypt());
                departmentServiceImpl.updateDepartment(departmentDAO);
            });
        }
        businessDepartmentDAO.setDel(Constant.DEL);
        businessDepartmentDAO.setUpdateBy(AuthUtil.getUserIdDecrypt());
        businessDepartmentDAO.setUpdateTime(LocalDateTime.now());
        businessDepartmentDAO.setId(id);
        int result = businessDepartmentServiceImpl.updateBusinessDepartment(businessDepartmentDAO);
        //清空空间业务部门指派
        //获取业务部门的未指派id
        String unassignedId = businessDepartmentServiceImpl.getUnassignedId(Constant.UNASSIGNED, projectId);
        //整理参数
        SpaceUnassignedDTO unassignedDTO = new SpaceUnassignedDTO();
        unassignedDTO.setBusinessId(id)
                .setUnassignedId(unassignedId);
        spaceServiceImpl.updateDepartment(unassignedDTO);

        return result == 1 ? ReturnMsg.consSuccess(Constant.DEL_SUCCESS) : ReturnMsg.consFail(Constant.DEL_FAIL);
    }

    /**
     * 查询业务部详情
     *
     * @param id
     * @author xuehao
     * @createDate 2022/3/9 15:21
     * @retuen ReturnMsg
     */
    @PostMapping("/getBusinessDepartment")
    public ReturnMsg getBusinessDepartment(String id) {
        try {
            ParamCheck.idCheck(id);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        BusinessDepartmentDetailVO businessDepartmentDetailVO = businessDepartmentServiceImpl.getBusinessDepartment(id);
        return ReturnMsg.consSuccess(businessDepartmentDetailVO);
    }


    /**
     * 业务部-部门列表
     *
     * @Author: haomeijie
     * @Date: 2022/3/22 18:39
     */
    @RequestMapping("/listBusinessDepartment")
    public ReturnMsg listBusinessDepartment(String projectId) {
        String name = "未指派";
        List<ListBusinessDepartmentVO> departmentVOS = businessDepartmentServiceImpl.listBusinessDepartment(name, projectId);
        return ReturnMsg.consSuccess(departmentVOS);

    }
}
